package cn.ynrcb.personnelfile.sysadmin.service.impl;

import cn.ynrcb.personnelfile.common.utils.MapUtils;
import cn.ynrcb.personnelfile.sysadmin.dao.SysUserRoleDao;
import cn.ynrcb.personnelfile.sysadmin.entity.SysUserRoleEntity;
import cn.ynrcb.personnelfile.sysadmin.service.SysUserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 用户与角色对应关系
 */
@Service("sysUserRoleService")
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleDao, SysUserRoleEntity> implements SysUserRoleService {

    @Override
    public void saveOrUpdate(Long userId, List<Integer> roleIdList) {
        //先删除用户与角色关系
        this.removeByMap(new MapUtils().put("user_id", userId));

        if (roleIdList == null || roleIdList.size() == 0) {
            return;
        }

        //保存用户与角色关系
        for (Integer roleId : roleIdList) {
            SysUserRoleEntity sysUserRoleEntity = new SysUserRoleEntity();
            sysUserRoleEntity.setUserId(userId);
            sysUserRoleEntity.setRoleId(roleId);

            this.save(sysUserRoleEntity);
        }
    }

    @Override
    public List<Integer> queryRoleIdList(Long userId) {
        return baseMapper.queryRoleIdList(userId);
    }

    @Override
    public int deleteBatch(Integer[] roleIds) {
        return baseMapper.deleteBatch(roleIds);
    }

    @Override
    public int checkByRoleId(List<Integer> asList) {
        return baseMapper.checkByRoleId(asList);
    }

    @Override
    public void deleteByUserId(List<Long> asList) {
        baseMapper.deleteByUserId(asList);
    }

    @Override
    public void saveUserAndRole(Long userId, int i) {
        this.removeByMap(new MapUtils().put("user_id", userId));
        SysUserRoleEntity sysUserRoleEntity = new SysUserRoleEntity();
        sysUserRoleEntity.setUserId(userId);
        sysUserRoleEntity.setRoleId(i);
        this.save(sysUserRoleEntity);
    }
}
